package com.msl.tool;

import android.util.Log;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileIO {

    static String tag="FileIO";

    public static void writeFile( String content) {
        try {
            content=content+"\n";
            File file = new File("/mnt/sdcard/mls","code.txt");
            FileOutputStream fileOutputStream = new FileOutputStream(file,true);
            fileOutputStream.write(content.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readFile( ) {
        String content = ""; //文件内容字符串
        //打开文件
        File file = new File("/mnt/sdcard/mls/code.txt");
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (file.isDirectory())
        {
            return "";
        }
        else
        {
            try {
                InputStream instream = new FileInputStream(file);
                if (instream != null)
                {
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);
                    String line;
                    //分行读取
                    while (( line = buffreader.readLine()) != null) {
                        content += line + "\n";
                    }
                    Log.e(tag,content);
                    instream.close();
                }
            }
            catch (Exception e)
            {
                Log.e(tag, e.getMessage());
            }
            finally {
                return content ;
            }
        }
    }

}
